package at.fhooe.obd.gui;


import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DialRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.DialRenderer.Type;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class GaugeGraph
{
	private static final String TAG = "OBD_Client";
	CategorySeries category = null;
	DialRenderer renderer = null;
	GraphicalView view = null;
	String name = null;
	
//	Intent intent = null;
	
	private boolean init(String name, String displayLabel, 
			double minVal, double maxVal, double initVal)
	{
		// nothing to check here...
		this.name = name;
		
		category = new CategorySeries(name);
	    category.add(displayLabel, initVal);
	    
	    renderer = new DialRenderer();
	    renderer.setChartTitleTextSize(50);
	    renderer.setLabelsTextSize(15);
	    renderer.setLegendTextSize(15);
	    renderer.setMargins(new int[] {20, 30, 15, 0});
	    
	    SimpleSeriesRenderer r = new SimpleSeriesRenderer();
	    r.setColor(Color.BLUE);
	    renderer.addSeriesRenderer(r);
	   
	    renderer.setLabelsTextSize(20);
	    renderer.setLabelsColor(Color.WHITE);
	    renderer.setShowLabels(true);
	    renderer.setVisualTypes(new DialRenderer.Type[] {Type.NEEDLE});
	    renderer.setMinValue(minVal);
	    renderer.setMaxValue(maxVal);
	    renderer.setApplyBackgroundColor(true);
	    renderer.setBackgroundColor(Color.BLACK);
		
		return true;
	}
	
	public Intent getIntent(Context context, String name, String displayLabel, 
			double minVal, double maxVal, double initVal)
	{
		if (!init(name, displayLabel, minVal, maxVal, initVal))
		{
			return null;
		}
		
		Intent intent = ChartFactory.getDialChartIntent(context, category, renderer, name);
		return intent;
	}
	
	public GraphicalView getView(Context context, String name, String displayLabel, 
			double minVal, double maxVal, double initVal)
	{
		if (!init(name, displayLabel, minVal, maxVal, initVal))
		{
			return null;
		}
		
		GraphicalView view = ChartFactory.getDialChartView(context, category, renderer);
		this.view = view;
		return view;
	}
	
	/**
	 * Uodate the gauge
	 * @param currentVal
	 */
	public void updateData(double currentVal)
	{
		// TODO: Test if that works...
		category.set(0, name, currentVal);
		
		if (this.view != null)
			view.repaint();
		
	}
}
